﻿
var APP=is_app();
var app_url = location.origin;
 var RATE = Number($.cookie("ParcelRate"));
 var TEAM=0;
 var PlaceOrder=false;//代下单
var indexCount=1;
var ends=true;
(function () {
    var parcels = {
        init: function () {       	
        	args = GetUrlParms();
            localStorage.removeItem('das');
			if(args["team"]!=undefined){						
				 TEAM = args["team"];//产品ID					
			} 
			if(args["cid"]!=undefined){						
				 Customer_id = args["cid"];	
				 PlaceOrder=true;//代下单
			} 
            this.bindEvents();

            //筛选条件

            loadInlays();


        },
        bindEvents: function () { 
            var self = this;
             //筛选
            $("#term_app_btn").on("click",function(){
            	if(PlaceOrder==true){
            		location.href="/Customer/ParcelFilter?team=3&cid="+Customer_id;
            	}else{
            		location.href="/Customer/ParcelFilter?team="+TEAM;
            	}
            });
             //app
           $("#parcels").on("click","#app_link_item",function(){
           		var link_url=$(this).attr("data-src");
                var das={
                    productno :$(this).parent().attr('data-productno'),
                    productsupplier :$(this).parent().attr('data-productsupplier')
                };
           		if(APP){
           			var appJson={};
           			appJson.title=_("统包详情");
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
           		}else{
           		    localStorage.setItem('das',JSON.stringify(das));
           			location.href=link_url;
           		}       
           });
           //点击菜单
			$("#show_menu_btn").on("click",function(){
				$("#assistant_menu_box").toggle();
			});
			//关闭菜单
			$("#assistant_menu_box").on("click",function(){
				$("#assistant_menu_box").hide();
			});
			//新增
			$("#add_btn").on("click",function(){
				var link_url="/Team/ParcelManagement?type=2";
				if(APP){
					var appJson={};
           			appJson.title=_("统包新增");
           			appJson.url=app_url+link_url;	
           			YiYi_APP.showNewPage(JSON.stringify(appJson));
				}else{
					location.href=link_url;
				}
			});
           
        }


    };
    parcels.init();
})();
//加载数据
 function loadInlays(pageIndex,pageSize) {
     indexCount++;
    myApp.showIndicator();//显示加载中
    var self = this;
    var params=PARAMS;
    if(params==null){
        params={};
    }
    if(TEAM==2){
        params.IsMyStock=true;//现货
    }else{
        params.OnShelves = "1";
    }
    if(TEAM==0){
        params.CustomerID = CustomerID;//客户ID
    }
    if(TEAM==3){
        params.UserID=UserID;
    }
    params.useDict = true;
     params.pageSize = pageSize?pageSize:50;
     params.pageIndex = pageIndex?pageIndex:1;
     params.limit=pageSize?pageSize:50;
    params.GoodsType=2;
    // var params = JSON.stringify(params);
    var params = JSON.stringify(params);
    var url ="/Team/GetProductData";
    $.ajax({
        url: url,
        data: params,
        type: "POST",
        contentType: "application/json; charset=utf8",
        success: function (result) {
            tempSupplier = 0;
            var res=eval('(' + result + ')')
            if (res.msgcode == true) {
                window.GOODS_ITEMS = res.rows;
                var dtRenderStart = new Date().getTime();
                isSearching = false;

                if (res.msginfo.rows.length == 0) {
                    $("#parcels").html('<div class="no_data_tip" style="color:red">'+_("没有符合条件的结果")+'</div>');
                } else {
                    var parcels = [];

                    for (var i = 0; i < res.msginfo.rows.length; i++) {
                        var row = res.msginfo.rows[i];
                        parcels.push(
                            '<li class="item" data="', row.Supplier, '" itemtype="" itemid="', row.ID, '" data-ProductNo="',row.OriginSN,'"  data-ProductSupplier="',row.Supplier,'">'
                        )
                        if(TEAM==1 || TEAM ==2){
                            parcels.push(
                                '<a href="javascript:void(0)" data-src=',"/","Team/ParcelDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
                            )
                        }else if(TEAM==3){
                            parcels.push(
                                '<a href="javascript:void(0)" data-src=',"/","Team/ParcelDetails/?ID=",row.ID,"&team=",TEAM,PlaceOrder?"&cid="+Customer_id:"", "&userName=",getUrlParam('userName'),' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
                            )
                        }else{
                            parcels.push(
                                '<a href="javascript:void(0)" data-src=',"/","Customer/ParcelDetails/?ID=",row.ID,"&team=",TEAM,"&typeNo=",1,PlaceOrder?"&cid="+Customer_id:"",' id="app_link_item" data-type="'+GetCategory(row.Category)+'">'
                            )
                        }

                        //库存权限start
                        if(TEAM==0){
                            parcels.push(
                                '<div  class="image_app_box">',
                                '<img class="product_img lazy" data-src="', imgSrc(row.Images), '" src="'+imgSrc(row.Images)+'" alt="统包"/>',
                                '</div>'
                            )
                            parcels.push(
                                '<div class="product_data">',
                                '<h4>'
                            );
                            if(RMBPrice_td_new==true){
                                parcels.push(
                                    '<span class="CurrentRMB">￥<b title="价格">'+formatRMBPrice(row.Price*RATE)+'</b></span>'
                                )
                            }
                            if(price_td_new==true){
                                parcels.push(
                                    '<span class="CurrentUSD">$<b title="价格">'+formatUSDPrice(row.Price)+'</b></span>'
                                )
                            }

                            parcels.push(
                                '</h4>'+
                                '<div class="data_box"><div>'
                            )
                            parcels.push(
                                '<b title="类型">'+GetCategory("parcels",row.Category)+'</b>'
                            )
                            parcels.push(
                                '<b title="形状">'+GetText("parcels", row.Category, row.Shape, "Shape")+ '</b>'
                            )
                            parcels.push(
                                '<b title="重量">'+row.Weight+'CT</b>'
                            )

                            parcels.push(
                                '</div><div>'
                            )
                            parcels.push(
                                '<b title="颜色">'+ GetText("parcels", row.Category, row.Color, "Color")+ '</b>'
                            )
                            parcels.push(
                                '<b title="净度">'+row.Clarity+'</b>'
                            )

                            parcels.push(
                                '</div>'+
                                '<div>')
                            if(place_td_new==true){
                                parcels.push(
                                    '<b title="所在地">'+ GetText("parcels", row.Category, row.Place, "Place")+ '</b>'
                                )
                            }

                            parcels.push(
                                '</div>'+
                                '</div>'+
                                '</div>'+
                                '</a>'+
                                '</li>'
                            )
                        }else if(TEAM==1 ||TEAM==3){

                            parcels.push(
                                '<div  class="image_app_box">',
                                '<img class="product_img lazy" data-src="', imgSrc(row.Images), '" src="'+imgSrc(row.Images)+'" alt="统包"/>',
                                '</div>'
                            )
                            parcels.push(
                                '<div class="product_data">',
                                '<h4>'
                            );

                            parcels.push(
                                menu_ordering_parcels_ConfirmPrice?'<span class="CurrentRMB">￥<b title="价格">'+formatRMBPrice(row.Price*RATE)+'</b></span>':''
                            )


                            parcels.push(
                                menu_ordering_parcels_ConfirmUSD?'<span class="CurrentUSD">$<b title="价格">'+formatUSDPrice(row.Price)+'</b></span>':''
                            )


                            parcels.push(
                                '</h4>'+
                                '<div class="data_box"><div>'
                            )

                            parcels.push(
                                menu_ordering_parcels_Category?'<b title="类型">'+GetCategory("parcels",row.Category)+'</b>':''
                            )

                            parcels.push(
                                menu_ordering_parcels_Shape?'<b title="形状">'+GetText("parcels", row.Category, row.Shape, "Shape")+ '</b>':''
                            )


                            parcels.push(
                                menu_ordering_parcels_Weight?'<b title="重量">'+row.Weight+'CT</b>':''
                            )


                            parcels.push(
                                '</div><div>'
                            )

                            parcels.push(
                                menu_ordering_parcels_Color?'<b title="颜色">'+ GetText("parcels", row.Category, row.Color, "Color")+ '</b>':''
                            )


                            parcels.push(
                                menu_ordering_parcels_Clarity?'<b title="净度">'+row.Clarity+'</b>':''
                            )

                            parcels.push(
                                '</div>'+
                                '<div>')

                            parcels.push(
                                menu_ordering_parcels_Place?'<b title="所在地">'+ GetText("parcels", row.Category, row.Place, "Place")+ '</b>':''
                            )

                            parcels.push(
                                '</div>'+
                                '</div>'+
                                '</div>'+
                                '</a>'+
                                '</li>'
                            )

                        }else if(TEAM==2){
                            parcels.push(
                                '<div  class="image_app_box">',
                                '<img class="product_img lazy" data-src="', imgSrc(row.Images), '" src="'+imgSrc(row.Images)+'" alt="统包"/>',
                                '</div>',
                                '<div class="product_data">',
                                '<h4>',
                                '<span class="CurrentRMB">￥<b title="价格">',returnRMBPrice(row),'</b></span>',
                                returnPrice(row),
                                '</h4>',
                                '<div class="data_box">',
                                '<div>',
                                '<b title="类型">',GetCategory("parcels",row.Category),'</b>',
                                '<b title="形状">', GetText("parcels", row.Category, row.Shape, "Shape"), '</b>',
                                '<b title="重量">',row.Weight,'CT</b>',
                                '</div><div>',
                                '<b title="颜色">', GetText("parcels", row.Category, row.Color, "Color"), '</b>',
                                '<b title="净度">',row.Clarity,'</b>',
                                '</div>',
                                '<div>',
//		                               					'<b title="供应商">',ConvertSupplier(row),'</b>',
                                '<b title="所在地">', GetText("parcels", row.Category, row.Place, "Place"), '</b>',
                                '</div>',
                                '</div>',
                                '</div>',
                                '</a>',
                                '</li>'
                            );
                        }

                    }

                    $("#parcels").append(parcels.join(''));

                    //懒加载
                    var n = 0,
                        imgNum = $("#parcels li").length,
                        img = $('#parcels li .product_img');
                    // myApp.initImagesLazyLoad(img)
                    $$('img.lazy').trigger('lazy');
                    //图片加载出来时居中显示 放在 framework.js
                    $(".image_app_box .product_img").load(function(){
                        //图片加载出来时居中显示  产品图  统包
                        var w=$(this).width();
                        var h=$(this).height();
                        var docu_w=$(window).width();
                        if(docu_w>760){
                            var screenW=244;
                        }else{
                            var screenW=docu_w/2;
                        }
                        if(w>h){
                            var w_scale=w/screenW;
                            var now_h=h/w_scale;
                            var pa_top=screenW/2-now_h/2;
                            $(this).css({
                                "width":"100%",
                                "padding-top":pa_top+"px",
                                "padding-bottom":pa_top+"px"
                            });
                        }else if(h>w){
                            var h_scale=h/screenW;
                            var now_w=w/h_scale;
                            var pa_left=screenW/2-now_w/2;
                            $(this).css({
                                "height":"100%",
                                "padding-left":pa_left+"px",
                                "padding-right":pa_left+"px"
                            });
                        }else{
                            $(this).css({
                                "height":"100%",
                                "width":"100%"
                            });
                        }

                    });


                    currentShow();  //币种设置
                }

                console.info(new Date().getTime() - dtRenderStart + "ms");
            } else {
                toast(res.msginfo);
            }
            myApp.hideIndicator();//隐藏加载中
            if(res.msginfo.rows.length>0&&res.msginfo.rows.length<50){
                ends=false;
                $('#endload').text('已全部加载完毕')
            }else {
                ends=true;
                $('#endload').text('')
            }
        }
    });
}
//没权限返回的价格
function returnPrice(data){
	//if(data.Price || data.Price==0){
		//var val=data.Weight?(((data.Price)/(data.Weight)).toFixed(2)):0;
		var priceHtml= ['<span class="CurrentUSD">$<b title="单价">'+formatUSDPrice(data.Price)+'</b></span>'].join('');
		return priceHtml;		
	//}else{
	//	return '<span style="font-size:14px;">$<b title="价格">权限不足</b></span>';	
	//}
}
function returnRMBPrice(data){
	if(data.Price || data.Price==0){
		//return data.Weight?((((data.Price)/(data.Weight)))*RATE).toFixed(1):0;
		return formatRMBPrice(data.Price*RATE);
	}else{
		return '-';	
	}
}
//优先显示简称
function ConvertSupplier(data) {
//	if(data.Supplier != undefined) {
//		if(data.ShortName != "") {
//			return data.ShortName;
//		} else {
			return data.Supplier;
//		}
//	} else
//		return "-";
}
//返回图片链接
function imgSrc(images) {
    if (images) {
        images = images.split('|');
        var small = images[0];
        var big = small.replace('800x800', '1920x1920');
        return small;
    }
    return '../images/no_img.jpg';//无图时返回默认图片
}




//显示图片
function ShowImg(src) {
    if (src) {
        var big = src.replace('800x800', '1920x1920');
        return ['<a href="' + src + '"><img src="' + src + '" class="parcel_img" style="width:100%;"/></a>'].join('');
    }
    else
        return "";
}
